package com.lzx.iteam;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PhotoUtil;

import java.io.File;

/**
 * Created by zhangshuai on 15-2-28.
 * 聊天拍照过滤页面
 */
public class TakePhotoActivity extends Activity {

    private String mTakePhotoImagePath;
    public static final  int TAKE_PHOTO = 4101;
    public static final  int BACK_WHITOUT = 4102;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTakePhotoImagePath = PhotoUtil.doTakePhoto(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ( resultCode != RESULT_OK){
            finish();
            return;
        }
        switch (requestCode){
            case PhotoUtil.CAMERA_WITH_DATAA:
                if(!"".equals(mTakePhotoImagePath)){

//                    mTakePhotoImagePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Yun/Images/"+mTakePhotoImagePath;
                    Bitmap bitmap = PhotoUtil.readBitmapFromPath(this,mTakePhotoImagePath,false);
                    int degree = PhotoUtil.getExifOrientation(mTakePhotoImagePath);
                    if(degree != 0){
                        bitmap = PhotoUtil.rotaingImageView(bitmap,degree);
                        try {
                            File file = new File(mTakePhotoImagePath);
                            file.delete();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        String tempPath = PhotoUtil.saveBitmaptoSdCard(bitmap,this,"/Yun/Images/","");
                        mTakePhotoImagePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Yun/Images/"+tempPath;
                    }
                    if(mTakePhotoImagePath != null && !"".equals(mTakePhotoImagePath)){
                        Intent intent = new Intent(this,ImagePagerActivity.class);
                        intent.putExtra(Constants.IMAGE_PATH, mTakePhotoImagePath);
                        intent.putExtra(Constants.IMAGE_ORIGIN,Constants.IMAGE_ORIGIN_LOCAL_STRING);
                        startActivityForResult(intent,TAKE_PHOTO);
                    }
                }
                break;
            case TAKE_PHOTO:
                mTakePhotoImagePath = data.getStringExtra(Constants.IMAGE_PATH);
                Intent intent = new Intent();
                intent.putExtra(Constants.IMAGE_PATH,mTakePhotoImagePath);
                setResult(RESULT_OK,intent);
                finish();
                break;

        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO 红米手机拍照 有时页面会被销毁 此时需要保存拍照地址
        outState.putString("pic_path", mTakePhotoImagePath);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO 取出保存的拍照地址
        mTakePhotoImagePath = savedInstanceState.getString("pic_path");
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        setResult(BACK_WHITOUT);
        this.finish();
        return super.onKeyDown(keyCode, event);
    }
}
